package com.kfm.base.collection;

import java.util.*;

public class Exer02 {

    public static void main(String[] args) {
        String s1 = "acdd";
        String s2 = "adcf";

        // 6 ^ a ^ a ^ c ^ d ^ d ^ c ^ d ^ d

        System.out.println(isSync3(s1, s2));

    }

    public static int binarySearch(List<Integer> list, Integer value) {
        if (list == null) {
            throw new NullPointerException("集合不能为 null");
        }
        // 起始点、中间
        int start = 0;
        int end = list.size() - 1;

        while (start <= end) {
            int middle = (start + end) / 2;

            if (list.get(middle) == value) {
                return middle;
            } else if (list.get(middle) > value) {
                end = middle - 1;
            } else {
                start = middle + 1;
            }
        }

        return -1;
    }

    public static String reverseByWhiteSpace(String str) {
        if (str == null) {
            throw new NullPointerException();
        }

        // 去掉左右两边的空格
        str = str.trim(); // the  sky  is   blue
        // 以 空格 将字符串分隔开
        String[] strings = str.split(" ");// the "" sky "" is "" "" blue


        // 数组反转
        StringBuilder sb = new StringBuilder();

        for (int i = strings.length - 1; i >= 0; i--) {
            // 多余的空格不处理
            if (strings[i].length() != 0){
                sb.append(strings[i]).append(" ");
            }
        }

        // 去掉最后一个空格
        String substring = sb.substring(0, sb.length() - 1);

        return substring;
    }


    public static boolean isSync(String str1, String str2){
        if (str1 == null || str2 == null){
            throw new NullPointerException();
        }

        // 长度不一样一定不是
        if (str1.length() != str2.length()){
            return false;
        }

        Map<Character, Integer> map1 = new HashMap<>();
        Map<Character, Integer> map2 = new HashMap<>();

        char[] charArray = str1.toCharArray();
        for (char c : charArray){
            map1.put(c, map1.getOrDefault(c, 0) + 1);
        }

        char[] charArray2 = str2.toCharArray();
        for (char c : charArray2){
            map2.put(c, map2.getOrDefault(c, 0) + 1);
        }

        return map1.equals(map2);

//        Set<Map.Entry<Character, Integer>> entries = map1.entrySet();
//
//        for (Map.Entry<Character, Integer> entry : entries){
//            Character key = entry.getKey();
//            Integer value = entry.getValue();
//            // 判断值是否一样
//            if (value != map2.get(key)){
//                return false;
//            }
//        }

//        return true;
    }

    public static boolean isSync2(String str1, String str2){
        if (str1 == null || str2 == null){
            throw new NullPointerException();
        }

        // 长度不一样一定不是
        if (str1.length() != str2.length()){
            return false;
        }

        char[] chars1 = str1.toCharArray();
        char[] chars2 = str2.toCharArray();

        Arrays.sort(chars1);
        Arrays.sort(chars2);

        String s1 = new String(chars1);
        String s2 = new String(chars2);
        return Arrays.equals(chars1, chars2);
    }

    public static boolean isSync3(String str1, String str2){
        if (str1 == null || str2 == null){
            throw new NullPointerException();
        }

        // 长度不一样一定不是
        if (str1.length() != str2.length()){
            return false;
        }

        int result = 6;
        for (int i = 0; i < str1.length(); i++) {
            result = result ^ str1.charAt(i) ^ str2.charAt(i);
        }

        return result == 6;
    }


    public static boolean isTrue(int[] nums1, int num){
        for (int i = 0; i < nums1.length; i++) {
            for (int j = i + 1; j < nums1.length; j++) {
                if (nums1[i] == nums1[j] && Math.abs(i - j) <= num){
                    return true;
                }
            }
        }
        return false;
    }
}
